package com.mmy.controller.bidtender;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.bidtender.TBidInvite;
import com.mmy.domain.bidtender.VBidInvite;
import com.mmy.domain.constparameter.TConstParameter;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.bidtender.BidInviteServiceImpl;
import com.mmy.service.impl.constparameter.ConstParameterServiceImpl;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.sys.SysOrgServiceImpl;
import com.mmy.service.permissionmenu.impl.PermissionServiceImpl;
import com.mmy.service.permissionmenu.impl.UserPermissionServiceImpl;

@Controller
public class BidInviteController {
	@Autowired
	BidInviteServiceImpl bidInviteServiceImpl;
	@Autowired
	SysOrgServiceImpl sysOrgServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	ConstParameterServiceImpl constParameterServiceImpl;
	@Autowired
	Common common;
	@Autowired
	UserPermissionServiceImpl userPermissionServiceImpl;
	@Autowired
	PermissionServiceImpl permissionServiceImpl;

	/**
	 * 函数功能说明： 个人中心显示本人发起的招标信息
	 *
	 * @param 参数说明 vBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */

	/* 显示招标基本信息首页 */
	@RequestMapping(value = "/myinvite")
	public String MyInvite(String pageNumber, String pageSize, Model model, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		/* 获得所有招标信息 */
		List<VBidInvite> lstBidInvite = bidInviteServiceImpl.findByCreator(pageNumber, pageSize, sessionSysUser);
		model.addAttribute("lstBidInvite", lstBidInvite);
		HashMap<String, Integer> mapPage = bidInviteServiceImpl.mapInvite(pageNumber, pageSize, sessionSysUser);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		return "permanagement/MyInvite";
	}

	/**
	 * 函数功能说明： 个人中心查看招标信息详情
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */

	@RequestMapping(value = "/myinviteinfo")
	public String toMyInviteInfo(HttpServletRequest request, Model model) {
		VBidInvite dbBidInvite = bidInviteServiceImpl.findByInviteID(request.getParameter("InviteID"));
		model.addAttribute("dbBidInvite", dbBidInvite);
		return "permanagement/MyInviteInfo";
	}

	/**
	 * 函数功能说明： 个人中心删除招标信息
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping("/deletemyinvitechecked")
	public String deleteMyInviteChecked(HttpServletRequest request, Model model) {
		VBidInvite dbBidInvite = bidInviteServiceImpl.findByInviteID(request.getParameter("iInviteID"));
		if (dbBidInvite.getCount() == null) {
			bidInviteServiceImpl.deleteByInviteID(request.getParameter("iInviteID"));
			model.addAttribute("msg", "撤标成功");
		} else {
			model.addAttribute("msg", "撤标失败");
		}
		return "redirect:myinvite";
	}

	/**
	 * 函数功能说明：后台招投标管理 显示招标基本信息
	 *
	 * @param 参数说明 vBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改人hanxl
	 * @date 创建日期 2020.11.18 修改时间2021.02.10
	 */
	@RequestMapping("/backbidinviteindex")
	public String backBidInviteIndex(HttpSession session, Model model) {

		// 下拉框测试
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<VBidInvite> lstBidInvite = bidInviteServiceImpl.findBidInvite(sessionSysUser);
		model.addAttribute("lstBidInvite", lstBidInvite);
		// 2021.03.17 先暂时要求只能超级管理员可以审核
		/*
		 * // 判断角色里是否存在总经理角色 TConstRole dbConstRole =
		 * permissionServiceImpl.getByRoleName(); TSysUserRole dbSysUserRole =
		 * userPermissionServiceImpl.findByRoleName(sessionSysUser.getUserID(),
		 * dbConstRole.getRoleID()); model.addAttribute("dbSysUserRole", dbSysUserRole);
		 */

		return "webbackground/bidtendermanagement/BidInviteIndex";
	}

	/**
	 * 函数功能说明： 异步--近郊游列表数据
	 * 
	 * @param 参数说明 vBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.02.22
	 *
	 */
	@RequestMapping("/backbidinvitejson")
	@ResponseBody
	public String backBidInviteJson(HttpServletRequest request, HttpSession session, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 搜索条件
		String strInviteName = request.getParameter("inviteName");
		String strType = request.getParameter("type");
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");

		// 表格数据
		List<VBidInvite> lstBidInvite = bidInviteServiceImpl.findByOrgID(sessionSysUser, strInviteName, strType,
				startIndex, count);
		for (VBidInvite vBidInvite : lstBidInvite) {
			Integer iZoneID = vBidInvite.getZoneID();
			vBidInvite.setZoneName(constZoneServiceImpl.findLocation(iZoneID));
		}
		model.addAttribute("lstBidInvite", lstBidInvite);
		// 数据总数量
		int total = bidInviteServiceImpl.queryBaseCount(sessionSysUser, strInviteName, strType);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstBidInvite);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	/**
	 * 函数功能说明：后台 查看招标信息详情
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.11.19
	 */

	@RequestMapping(value = "/bidinviteinfo")
	public String toBidInviteInfo(HttpServletRequest request, Model model) {
		/* 查看招标基本详细信息 */
		VBidInvite dbBidInvite = bidInviteServiceImpl.findByInviteID(request.getParameter("iInviteID"));
		model.addAttribute("dbBidInvite", dbBidInvite);
		// 通过目的地查询省和市
		String strProvince = constZoneServiceImpl.findProvince(dbBidInvite.getZoneID()).getZoneName();
		String strCity = constZoneServiceImpl.findCity(dbBidInvite.getZoneID()).getZoneName();
		model.addAttribute("strProvince", strProvince);
		model.addAttribute("strCity", strCity);
		return "webbackground/bidtendermanagement/BidInviteInfo";
	}

	/**
	 * 函数功能说明： 新建招标信息
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19
	 */
	@RequestMapping(value = "/tocreatebidinvite")
	public String toCreateBidInvite(Model model) {
		// 查询所有区域省市区
		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		// 参数设置查询招标预算最大值
		String maxBudget = "招投标预算";
		TConstParameter dbConstParameter = constParameterServiceImpl.getByText(maxBudget);
		model.addAttribute("dbConstParameter", dbConstParameter);
		return "webbackground/bidtendermanagement/CreateBidInvite";
	}

	// 输入创建信息提交
	@RequestMapping(value = "/savecreatebidinvite")
	public String saveCreateBidInvite(TBidInvite tBidInvite, String strOutDateTime, String strBackDateTime,
			String strBeginDateTime, String strEndDateTime, HttpSession session) throws ParseException {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		bidInviteServiceImpl.createInvite(tBidInvite, strOutDateTime, strBackDateTime, strBeginDateTime, strEndDateTime,
				sessionSysUser);
		return "redirect:backbidinviteindex";
	}

	/**
	 * 函数功能说明：后台 修改招标信息
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19
	 */
	// 点击修改按钮进入修改页面
	@RequestMapping("/toupdatebidinvite")
	public String toUpdateBidInvite(HttpServletRequest request, Model model) {
		VBidInvite dbBidInvite = bidInviteServiceImpl.findByInviteID(request.getParameter("iInviteID"));
		model.addAttribute("dbBidInvite", dbBidInvite);
		String strOutDate = common.getTableDate(dbBidInvite.getOutDate(), UtilConstants.DateStyle.YYYY_MM_DD);
		model.addAttribute("strOutDate", strOutDate);
		String strBackDate = common.getTableDate(dbBidInvite.getBackDate(), UtilConstants.DateStyle.YYYY_MM_DD);
		model.addAttribute("strBackDate", strBackDate);
		String strBeginDate = common.getTableDate(dbBidInvite.getBeginDate(), UtilConstants.DateStyle.YYYY_MM_DD);
		model.addAttribute("strBeginDate", strBeginDate);
		String strEndDate = common.getTableDate(dbBidInvite.getEndDate(), UtilConstants.DateStyle.YYYY_MM_DD);
		model.addAttribute("strEndDate", strEndDate);
		// 通过目的地查询省和市
		int strProvinceID = constZoneServiceImpl.findProvince(dbBidInvite.getZoneID()).getZoneID();
		int strCityID = constZoneServiceImpl.findCity(dbBidInvite.getZoneID()).getZoneID();
		// 查询所有省
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		// 根据省ID查出该省所有的市
		List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(strProvinceID);
		model.addAttribute("lstConstZoneCity", lstConstZoneCity);
		// 根据省ID查出该市所有的区
		List<TConstZone> lstConstZoneDistrict = constZoneServiceImpl.findDistricts(strCityID);
		model.addAttribute("lstConstZoneDistrict", lstConstZoneDistrict);

		model.addAttribute("strProvinceID", strProvinceID);
		model.addAttribute("strCityID", strCityID);
		// 参数设置查询招标预算最大值
		String maxBudget = "招投标预算";
		TConstParameter dbConstParameter = constParameterServiceImpl.getByText(maxBudget);
		model.addAttribute("dbConstParameter", dbConstParameter);

		return "webbackground/bidtendermanagement/UpdateBidInvite";
	}

	// 提交修改的招标信息
	@RequestMapping(value = "/saveupdatebidinvite")
	public String saveUpdateBidInvite(TBidInvite tBidInvite, String strOutDateTime, String strBackDateTime,
			String strBeginDateTime, String strEndDateTime, HttpSession session) throws Exception {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		bidInviteServiceImpl.updateInvite(tBidInvite, strOutDateTime, strBackDateTime, strBeginDateTime, strEndDateTime,
				sessionSysUser);
		return "redirect:backbidinviteindex";
	}

	/**
	 * 函数功能说明： 发布招标信息
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19
	 */
	@RequestMapping("/issuebidinvite")
	@ResponseBody
	public Byte issueBidInvite(HttpServletRequest request, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		Byte iState = bidInviteServiceImpl.updateState(request.getParameter("iInviteID"), sessionSysUser);
		return iState;
	}

	/**
	 * 函数功能说明：后台审核招标信息
	 *
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19 修改时间2021.03.17
	 */
	@RequestMapping(value = "/toreviewbidinvite")
	public String toReviewBidInvite(Model model) {
		// 获得所有需要审核的招标信息
		//
		List<VBidInvite> lstBidInvite = bidInviteServiceImpl.findByState();
		model.addAttribute("lstBidInvite", lstBidInvite);

		return "webbackground/bidtendermanagement/BidInviteReview";
	}

	/**
	 * 函数功能说明： 异步--近郊游列表数据
	 * 
	 * @param 参数说明 vBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.02.22
	 *
	 */
	@RequestMapping("/backbidinvitereviewjson")
	@ResponseBody
	public String backBidInviteReviewJson(HttpServletRequest request, HttpSession session, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 表格数据
		List<VBidInvite> lstBidInvite = bidInviteServiceImpl.findReviewByState(startIndex, count);
		for (VBidInvite vBidInvite : lstBidInvite) {
			Integer iZoneID = vBidInvite.getZoneID();
			vBidInvite.setZoneName(constZoneServiceImpl.findLocation(iZoneID));
		}
		model.addAttribute("lstBidInvite", lstBidInvite);
		// 数据总数量
		int total = bidInviteServiceImpl.queryBaseReviewCount();
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstBidInvite);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	/**
	 * 函数功能说明：后台审核招标信息 通过审核发布
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19
	 */

	@RequestMapping("/ispassbidinvitereview")

	public String passBidInviteReview(HttpServletRequest request, HttpSession session, String strAmount,
			String strInviteID, String strAccountState) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		bidInviteServiceImpl.updatePassState(strAmount, strAccountState, strInviteID, sessionSysUser);
		return "redirect:backbidinviteindex";
	}

	/**
	 * 函数功能说明： 前台显示招标信息 只显示登录人所属公司的所属区域
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19
	 */
	@RequestMapping(value = "/tofrontbidinviteindex")
	public String toFrontBidInviteIndex(HttpServletRequest request, Model model, String pageNumber, String pageSize,
			HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strInviteName = request.getParameter("strInviteName");
		String strType = request.getParameter("strType");
		String strBudget = request.getParameter("strBudget");
		List<VBidInvite> lstBidInvite = bidInviteServiceImpl.findByZoneID(pageNumber, pageSize, strInviteName, strType,
				strBudget, sessionSysUser);
		model.addAttribute("lstBidInvite", lstBidInvite);
		HashMap<String, Integer> mapPage = bidInviteServiceImpl.mapFrontBidInvite(pageNumber, pageSize, strInviteName,
				strType, strBudget, sessionSysUser);
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		model.addAttribute("strType", strType);
		model.addAttribute("strBudget", strBudget);
		model.addAttribute("strInviteName", strInviteName);
		return "bidtender/BidInviteIndex";
	}

	/**
	 * 函数功能说明： 前台显示招标详细信息
	 *
	 * @param 参数说明 tBidInvite
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.11.19
	 */
	@RequestMapping(value = "/toinvitedetail")
	public String toInviteDetail(HttpServletRequest request, Model model) {
		/* 查看招标基本详细信息 */
		VBidInvite dbBidInvite = bidInviteServiceImpl.findByInviteID(request.getParameter("iInviteID"));
		model.addAttribute("dbBidInvite", dbBidInvite);
		// 通过目的地查询省和市
		String strProvince = constZoneServiceImpl.findProvince(dbBidInvite.getZoneID()).getZoneName();
		String strCity = constZoneServiceImpl.findCity(dbBidInvite.getZoneID()).getZoneName();
		model.addAttribute("strProvince", strProvince);
		model.addAttribute("strCity", strCity);
		return "bidtender/BidInviteInfo";
	}
}
